package com.gomyb.common.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author <a href="mailto:hellohesir@gmail.com">Mr_He</a>
 * @date 2018/11/4 0:49
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtil.applicationContext = applicationContext;
    }

    public static <T>T getBean(String name) {
        try {
            return (T)applicationContext.getBean(name);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
